package com.mornd.server.utils;

import com.mornd.server.pojo.Admin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author mornd
 * @dateTime 2022/6/10 - 19:41
 */
@Slf4j
public class SecurityUtil {
    /**
     * 获取当前登录用户
     * @return
     */
    public static Admin getCurrAdmin() {
        try {
            Admin principal = (Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return principal;
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    /**
     * 获取当前登录用户id
     * @return
     */
    public static Integer getCurrId() {
        return getCurrAdmin().getId();
    }
}
